<?php


namespace app\service;


use app\dao\RoomDao;
use app\lib\exception\ServiceException;
use think\facade\Db;

class RoomService
{
    private $RoomDao;
    private $FilesService;
    private $RoomPasswordService;
    private $RoomPriceService;
    private $UsingRoomTimeService;
    private $RoomTypeService;

    public function __construct()
    {
        $this->RoomDao = new RoomDao();
        $this->FilesService = new FilesService();
        $this->RoomPasswordService = new RoomPasswordService();
        $this->RoomPriceService = new RoomPriceService();
        $this->UsingRoomTimeService = new UsingRoomTimeService();
        $this->RoomTypeService = new RoomTypeService();
    }

    public function setRoomTypeService(){
        return new RoomTypeService();
    }

    public function setHotelService(){
        return new HotelService();
    }

    public function getRoom($id){
            $model = $this->RoomDao
            ->customQuery()
            ->withRoomPassword()
            ->filterById($id)
            ->getOne();
            $TypeModel = $this->RoomTypeService->getRoomType($model->type_id);
            $PriceModel = $this->RoomPriceService->getRoomPriceByRoomId($model->id);

            $model->type = $TypeModel->name;
            $model->describe = $TypeModel->describe;

            $model->price = $PriceModel->now_price;
            return $model;
    }

    public function getRoomList($page , $limit){
        return $this->RoomDao
            ->customQuery()
            ->withRoomTypeAndRoomPasswordAndRoomPrice()
            ->getList($page , $limit);
    }

    public function getRoomByTypeId($type_id){
        return $this->RoomDao
            ->customQuery()
            ->filterByTypeId($type_id)
            ->getAll();
    }

    public function editRoom($id , $data){
        $editData = [];
        $status = $data['status'] == 0 || $data['status'] == 1 ? false : true;
        empty($data['content'])||$editData['content'] = $data['content'];
        empty($data['deposit'])||$editData['old_price'] = $data['deposit'];
        $status || $editData['status'] = $data['status'];

        try {
            Db::startTrans();

            if (isset($data['type']) || isset($data['describe'])){
                $type_id = $this->RoomDao->find($id)->type_id;
                $TypeData = [];
                empty($data['type'])||$TypeData['type'] = $data['type'];
                empty($data['describe']) || $TypeData['describe'] = $data['describe'];
                $this->RoomTypeService->editRoomType($type_id , $TypeData);
            }

            if (isset($data['hotel_id'])){
                $hotel_model = $this->setHotelService()->getHotel($data['hotel_id']);
                if ($hotel_model == null) throw new ServiceException("酒店不存在");
                $editData['hotel_id'] = $hotel_model->id;
            }

            if (isset($data['img_url'])){
                $url = $this->FilesService
                    ->save2Oss($data['img_url'] , 'room');

                $editData['img_url'] = json_encode($url);
            }

            $this->RoomDao->edit($id , $editData);
            if (!empty($data['price']))$this->RoomPriceService
                ->editRoomPriceByRoomId($id , $data['price']);

            Db::commit();
        }catch (\Exception $exception){
            Db::rollback();
            throw new ServiceException($exception->getMessage());
        }
        return $this->getRoom($id);
    }

    public function addRoom($content , $type , $describe , $status , $deposit , $img_url , $price , $hotel_id){
        try {
            Db::startTrans();
            $url = $this->FilesService->save2Oss($img_url);
            $url = json_encode($url);

            $type_mode = $this->RoomTypeService->addRoomType($type , $describe);
            $hotel_model = $this->setHotelService()->getHotel($hotel_id);
            if ($hotel_model == null) throw new ServiceException("酒店不存在");

            $model = $this->RoomDao->save([
                    'content' => $content ,
                    'type_id' => $type_mode->id,
                    'old_price' => $deposit ,
                    'img_url' => $url ,
                    'hotel_id' => $hotel_model->id,
                    'status' => $status,
                ]);
            $this->RoomPriceService
                ->addRoomPrice($model->id , $price);
            Db::commit();
        }catch (\Exception $exception){
            Db::rollback();
            throw new ServiceException($exception->getMessage());
        }
        return $this->getRoom($model->id);
    }

    public function deleteRoom($id){
        try {
            Db::startTrans();
            $this->RoomTypeService->delRoomType($this->RoomDao->find($id)->type_id);
            $this->RoomDao->delete($id);
            $this->RoomPriceService
                ->delRoomPriceByRoomId($id);
            $this->RoomPasswordService
                ->delRoomPasswordByRoomId($id);
            Db::commit();
        }catch (\Exception $exception){
            Db::rollback();
            throw new ServiceException($exception->getMessage());
        }
    }
}